package com.openapi.openapi.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang.StringUtils;
import com.openapi.openapi.vo.ResData;
import com.openapi.openapi.controller.file.Base64File;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Base64;

@RestController
@RequestMapping("/file")
public class FileController {

    @Value("${server_config.img_local_path}")
    private String imgLocalPath;
    private static Pattern pattern = Pattern.compile("(data:.*;base64,)");

    @RequestMapping(value = "base64-upload", method = RequestMethod.POST)
    public ResData<String> base64Upload(@RequestBody Base64File base64File) {

        String originFileName = base64File.getFileName();
        String ext = "";
        int lastDot = StringUtils.lastIndexOf(originFileName, ".");
        if (lastDot > -1) {
            ext = originFileName.substring(lastDot);
        }

        String base64Data = getBase64Data(base64File.getData());
        byte[] data = Base64.getDecoder().decode(base64Data);

        String fileUrl = storeFile(ext, data);
        return new ResData<>(fileUrl);
    }

    @RequestMapping(value ="img/{imgName}", method = RequestMethod.GET)
    public void get(@PathVariable("imgName") String imgName,
                    HttpServletResponse response) throws IOException {

            System.out.println(imgName);
        InputStream is = new FileInputStream(imgLocalPath+imgName);
        OutputStream out = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len=is.read(buffer))!=-1) {
            out.write(buffer, 0, len);

        }

    }
//    @RequestMapping(value ="img", method = RequestMethod.GET)
//    public void get(@PathVariable("imgName") int imgName,
//                    HttpServletRequest request,
//                    HttpServletResponse response) throws IOException {
//        System.out.println(imgName);
//        InputStream is = new FileInputStream("C:\\"+imgName);
//        OutputStream out = response.getOutputStream();
//        byte[] buffer = new byte[1024];
//        int len = 0;
//        while((len=is.read(buffer))!=-1){
//            out.write(buffer, 0, len);
//        }
//    }

    private String storeFile(String ext,byte[]  data){
        String fname = System.currentTimeMillis() +ext;//保存照片的文件名:名字+格式，xxx.png

        File picture = new File(imgLocalPath, fname);//保存照片的文件
        FileOutputStream out = null;
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(data);
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(picture);
            int byteread;
            while ((byteread = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteread); // 文件写操作
            }
        }catch (Exception e){

        }
        return fname;

    }


    private String getBase64Data(String data) {
        String pre50 = data.substring(0, 50);
        Matcher matcher = pattern.matcher(pre50);
        if (matcher.find()) {
            return data.substring(matcher.group(1).length());
        } else {
            return data;
        }
    }


    public static void  main(String[] array){


    }

}
